<template>
  <div class="app-container">
    <el-button style="margin-bottom: 20px;" type="primary" @click="add(0)">添加管理员</el-button>
    <el-form :inline="true" style="float:right;" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input v-model="keyWord.keyWord" placeholder="请输入昵称/账户名搜索" />
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="getList()">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="tableData.data"
      stripe
      style="width: 100%"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        prop="id"
        label="ID"
      />
      <el-table-column
        prop="name"
        label="登录账号"
      />
      <el-table-column
        prop="nick_name"
        label="昵称"
      />
      <el-table-column
        prop="role_name"
        label="角色"
      />
      <el-table-column
        label="最近登录时间"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.last_login_time">{{ timestampToTime(scope.row.last_login_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
          <el-tag v-else type="info">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
      >
        <template v-if="scope.row.id != 1" slot-scope="scope">
          <el-button type="text" @click="add(scope.row)">编辑</el-button>
          <el-button type="text" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="text-align:center;margin-top:20px"
      background
      layout="prev,pager,next,sizes,total,jumper"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="keyWord.pageCount"
      :total="total"
      :current-page="keyWord.page"
      @current-change="currentChange"
      @size-change="sizeChange"
    />

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <div>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="角色">
            <el-select v-model="form.role_id" placeholder="请选择角色">
              <el-option v-for="(val,index) in roleList" :key="index" :label="val.name" :value="val.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="登录账号">
            <el-input v-model="form.name" placeholder="请输入登录账号" />
          </el-form-item>
          <el-form-item label="登录密码">
            <el-input v-model="form.password" placeholder="请输入登录密码（不修改放空）" />
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nick_name" placeholder="请输入昵称" />
          </el-form-item>
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="2">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button @click="sub()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { AdminUserList, AdminUserEdit, AdminUserGetRole, AdminUserDel } from '@/api/user'

export default {
  name: 'User',
  data() {
    return {
      tableData: [],
      loading: true,
      dialogVisible: false,
      total: 0,
      keyWord: {
        page: 1,
        pageCount: 10,
        keyWord: ''
      },
      form: {
        id: '',
        name: '',
        nick_name: '',
        password: '',
        role_id: '',
        status: 1
      },
      oldForm: {
        id: '',
        name: '',
        nick_name: '',
        password: '',
        role_id: '',
        status: 1
      },
      roleList: []

    }
  },
  beforeCreate() {

  },
  created() {
    this.getList()
    this.getRoleList()
  },
  methods: {
    sizeChange(e) {
      this.keyWord.pageCount = e
      this.keyWord.page = 1
      this.loading = true
      this.getList()
    },
    currentChange(e) {
      this.keyWord.page = e
      this.loading = true
      this.getList()
    },
    async getList() {
      this.loading = true
      this.tableData = []
      AdminUserList(this.keyWord).then(response => {
        this.loading = false
        this.tableData = response.data || []
        this.total = response.data.total || 0
      }).catch(() => {
        this.loading = false
      })
    },
    getRoleList() {
      AdminUserGetRole().then(res => {
        this.roleList = res.data
      })
    },
    timestampToTime(times) {
      var date = new Date(times * 1000) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + '-'
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      var D = date.getDate() + ' '
      var h = date.getHours() + ':'
      var m = date.getMinutes() + ':'
      var s = date.getSeconds()
      return Y + M + D + h + m + s
    },
    add(data) {
      if (data) {
        this.form.id = data.id
        this.form.role_id = data.role_id
        this.form.nick_name = data.nick_name
        this.form.name = data.name
        this.form.status = data.status
      } else {
        this.form = this.oldForm
      }
      this.dialogVisible = true
    },
    sub() {
      this.loading = true
      AdminUserEdit(this.form).then(res => {
        this.dialogVisible = false
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.getList()
      }).catch(() => {
        this.loading = false
      })
    },
    del(id) {
      this.$confirm('此操作将删除该节点和所有子节点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        AdminUserDel({ id: id }).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss">

</style>
